﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

using GolfSigma.Inventory;
using GolfSigma.Inventory.Model;

namespace GolfSigma.Inventory.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new instance of our client
            var client = new InventoryClient(
                "[Removed]",
                "[Removed]",
                new InventoryConfig()
                {
                    MaxErrorRetry = 2,
                    //ServiceURL = "http://ipv4.fiddler:54657",
                    ServiceURL = "http://inv.golfsigmaws.com",
                    ProxyHost = "localhost",
                    ProxyPort = 8888,
                    UserAgent = "GolfSigma.Inventory.Console"
                });

            //byte[] secretkey = new Byte[64];
            //RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            //rng.GetBytes(secretkey);
            //var key = Convert.ToBase64String(secretkey);

            // Try and get a teetime
            //var result = client.GetTeeTime(
            //    new GetTeeTimeRequest(
            //        new Guid("e3c0570fe3304fe29eb94befed8f59d0")
            //        )
            //    );

            // Try and get a search
            //var result = client.GetSearch(
            //    new GetSearchRequest(
            //        new Guid("9e5c514a833b45c6ba3689437db32613")
            //        )
            //    );

            // Try and get inventory by searchId
            //var result = client.GetInventory(
            //    new GetInventoryRequest(
            //        new Guid("5531441c1dc74511b637b4c510602170")
            //        )
            //    );

            // Try and get inventory in a single call by creating a search at the same time
            //var result = client.GetInventory(
            //        new PutSearchRequest()
            //        {
            //            Search = new Search()
            //            {
            //                ExternalId = "invClientLibrary",
            //                Date = DateTime.Now.AddDays(2).Date,
            //                MaxDaysOut = 1,
            //                Players = PlayerEnum.All,
            //                SearchType = SearchTypeEnum.SingleDay,
            //                TimeOfDay = new TimeSpan(9, 30, 0),
            //                Attributes = new List<TeeTimeAttributeEnum>()
            //                {
            //                    TeeTimeAttributeEnum.Holes18
            //                },
            //                CourseIds = new List<Guid>()
            //                {
            //                    new Guid("765bb849-4766-452a-aa0b-49a933dfb7bb")
            //                },
            //                Filters = new List<SearchFilter>()
            //                {
            //                    new SearchFilter(new Guid("90d280e6-a8e2-428a-992f-cb47b9c799d3"), "Interval", "30")
            //                }
            //            }
            //        }
            //    );

            // Try and get similar tee-times
            //var result = client.GetSimilarTeeTimes(
            //    new GetSimilarTeeTimeRequest(
            //        new Guid("f9189ef2feaf4a68a2a4fbc7c43fbf84")
            //        )
            //    );

            // Create a search
            //var result = client.PutSearch(
            //    new PutSearchRequest()
            //    {
            //        Search = new Search()
            //        {
            //            ApplicationId = Guid.NewGuid(),
            //            ExternalId = "invClientLibrary",
            //            Date = DateTime.Now.AddDays(7),
            //            MaxDaysOut = 1,
            //            Players = PlayerEnum.Threesome,
            //            SearchType = SearchTypeEnum.SingleDay,
            //            TimeOfDay = new TimeSpan(9, 30, 0),
            //            Attributes = new List<TeeTimeAttributeEnum>()
            //            {
            //                TeeTimeAttributeEnum.Holes18,
            //                TeeTimeAttributeEnum.MembersOnly
            //            },
            //            Filters = new List<SearchFilter>()
            //            {
            //                new SearchFilter(new Guid("22816dc4-538e-412e-8594-f46150e6520c"), "MinPrice", "20")
            //            }
            //        }
            //    });

            // Batch put multiple tee times
            //var teeTimes = new List<Model.TeeTime>();
            //var random = new Random();
            //for (int i = 0; i < 25; i++)
            //{
            //    teeTimes.Add(
            //        new TeeTime()
            //        {
            //            CourseId = new Guid("8DE1E16D-910B-49AD-909B-60FF507E0298"),
            //            MiscData = i.ToString(),
            //            Players = GetRandomPlayers(random),
            //            Time = GetRandomDate(random),
            //            TotalPricePerPlayer = GetRandomPrice(random),
            //            WebSiteId = new Guid("804A6464-B9AE-4A84-925E-246A741F85E2"),
            //            Attributes = new List<TeeTimeAttributeEnum>()
            //            {
            //                TeeTimeAttributeEnum.Holes18
            //            },
            //            Fees = new Dictionary<FeeEnum, float>()
            //            {
            //                { FeeEnum.Green, GetRandomPrice(random) },
            //                { FeeEnum.Cart, GetRandomPrice(random) }
            //            }
            //        });
            //}
            //client.BatchPutTeeTimes(
            //    new BatchPutTeeTimesRequest(teeTimes)
            //    );


            // Put a single tee times
            //var random = new Random();
            //var result = client.PutTeeTime(
            //    new PutTeeTimeRequest()
            //    {
            //        TeeTime = new TeeTime()
            //        {
            //            CourseId = new Guid("8DE1E16D-910B-49AD-909B-60FF507E0298"),
            //            MiscData = "feeTest",
            //            Players = PlayerEnum.All,
            //            Time = GetRandomDate(random),
            //            TotalPricePerPlayer = GetRandomPrice(random),
            //            WebSiteId = new Guid("804A6464-B9AE-4A84-925E-246A741F85E2"),
            //            Fees = new Dictionary<FeeEnum, float>()
            //            {
            //                { FeeEnum.Green, 45F },
            //                { FeeEnum.Cart, 10F }
            //            }
            //        }
            //    });

            // Create a reservation
            //var result = client.PutReservation(
            //    new PutReservationRequest()
            //    {
            //        Reservation = new Reservation()
            //        {
            //            ExternalId = "shaunWasHere",
            //            SearchId = new Guid("ac1034cc99f94a42be726d41cfe19ca1"),
            //            TeeTimeId = new Guid("beeac8b3b6e149599b9a1a310c2ff70f"),
            //        }
            //    });

            // Get an existing reservation
            //var result = client.GetReservation(
            //    new GetReservationRequest()
            //    {
            //        ReservationId = new Guid("66dacf5c2cb04d4683317f3a3aae5fcc")
            //    });
        }

        static DateTime GetRandomDate(Random random)
        {
            var randomSeconds = random.Next(0, 28800);

            DateTime minDate = DateTime.Now.AddDays(2).Date.AddHours(7);
            var result = minDate.AddSeconds(randomSeconds);

            return result.AddSeconds(-result.Second);
        }

        static float GetRandomPrice(Random random)
        {
            var rnd = new Random();
            var randomAddition = random.Next(0, 10000);

            float minPrice = 10;

            return minPrice + (randomAddition / 100F);
        }

        static PlayerEnum GetRandomPlayers(Random random)
        {
            var rnd = new Random();
            var randomAddition = random.Next(1, 5);

            switch (randomAddition)
            {
                case 1: return PlayerEnum.Single; break;
                case 2: return PlayerEnum.Twosome; break;
                case 3: return PlayerEnum.Threesome; break;
                case 4: return PlayerEnum.Foursome; break;
                case 5: 
                default:
                    return PlayerEnum.All; break;
            }
        }
    }
}
